//KeyInfo.java
package singletonAndMultiton;

/**
 * hold the database's key value.
 *@author:youtubezou
 *@version:2012-7-14
 */
public class KeyInfo {
	private int keyMax;
	private int keyMin;
	private int nextKey;
	private int poolSize;
	private String keyName;
	
	//use to simulate database's key
	private static int keyNum = 1000;
	
	public KeyInfo(int poolSize, String keyName){
		this.poolSize = poolSize;
		this.keyName = keyName;
		retrieveFromDB();
	}
	
	public int getNextKey(){
		if (nextKey > keyMax)
			retrieveFromDB();
		return nextKey++;
	}
	
	public int getKeyMax(){
		return keyMax;
	}
	
	public int getKeyMin(){
		return keyMin;
	}
	
	public String getName(){
		return keyName;
	}
	
	//fake function, use to simulate get data from database
	private void retrieveFromDB(){
		keyNum += poolSize;
		keyMax = keyNum;
		keyMin = keyMax - poolSize + 1;
		nextKey = keyMin;
	}
}
